<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class manager_win extends win_gtk {

	var $trayicon = FALSE;						// GTKStatusIcon object
	var $minimised = FALSE;						// Minimised to system tray

	var $actions = array();						// List of possible actions that can be done in trayicon
	

	public $win_name = 'manager';
	public $win_icon = WINDOW_ICON;
	public $win_title = APPNAME;
	
	public $statusbar = FALSE;					// Statusbar control
	public $statusbar_text=array();				// Statusbar test stored so can be resized

	public $control=array();					// All controls

	var $cust_name='';							// Display formatted customer name
	var $task_name='';							// Display formatted task name

	var $timesheets = FALSE;					// Full path to current timesheets file in use
	var $customers = FALSE;						// Full path to current customers file in use
	var $workers = FALSE;						// Full path to current workers file in use
	var $config = FALSE;						// Full path to current config file in use
	
//	var $data_types = FALSE;					// List of data types that can be attached to sessions
	var $xlate = FALSE;							// All text translations
	


	var $apps = array();
	var $modules = array();

	function __construct() {
		global $gl, $argv;
		
		$this->const_base = 10100;
		$this->make_constants();

	}


	/**
	*	Populate the customer list
	*	
	*	@param	
	*	@return	
	*	
	*/
	function customer_list_draw($customer_id=FALSE) {

		$ts = new Customers($this->customers);
		$customers = $ts->getCustomers();
		$ts->close();
		unset( $ts);
		
		$this->control['ts_customers']->clear();

		foreach ( $customers as $idx => $dat ) {
			$row_dat = array( $dat['id'], $dat['name'], $dat['company'] );
			$this->control['ts_customers']->append($row_dat);
		}

//$this->control['ts_customers']->widget->set_grid_lines(Gtk::TREE_VIEW_GRID_LINES_VERTICAL); 

		if ( $customer_id ) {
			// Make this customer selected as well
			$this->control['ts_customers']->set_selected('id',$customer_id);
		}



	}


	/**
	*	Set up customers frame display
	*	
	*	@param	
	*	@return	
	*	
	*/
	function customers() {
		global $gl;
		
		$customer_id = $this->selected_customer();
				
		if ( $customer_id ) {

			// Customer selected
			$customer_name = $this->control['ts_customers']->get_selected('name');
				
			// Load the Project/Task frame
			$this->project_list_draw($customer_id);				
				
			// Enable appropriate timer tools at bottom
			$this->enable_customer($customer_id);

					
		}
		else {
			// No selection
			$this->resetControls();				
		}
				
	
	}
	


	function disable($control) {
		$control = strtolower($control);
		$this->control[$control]->set_enabled(FALSE);	
	}
	
	function enable($control) {
		$control = strtolower($control);
//echo "[$control]\n";
		$this->control[$control]->set_enabled(TRUE);	
	}
	


	function enable_customer($customer_id) {
		// Enable all controls for a selected customer

		$this->enable('TS_NEW');
		$this->enable('TS_EDIT');
		$this->enable('TS_DELETE');

	}


	function enable_report_list() {
		$this->enable( 'TS_REPORT_LIST' );
	}

	function get_position() {

	}


	function restore_position() {

	}


	function command_line($argv) {

		// Dropped file - don't start app, pass to add_file instead
		$dropped_file = trim($argv[1]);

		log::debug('argv[1]:'.$argv[1]);
		log::debug('argv[2]:'.$argv[2]);

		$path_parts = pathinfo($dropped_file);
		$ext = trim(strtolower($path_parts['extension']));

		$ext_not_capture = array('rds','rdl','rdc','rdr');

		if ( $dropped_file <> '' && $dropped_file <> 'autostart' ) {
			if ( $argv[2] == 'clicked' || in_array($ext,$ext_not_capture)  ) {

				// Double clicked file from extension associations
				log::write('Clicked file : '.$dropped_file);
				$this->clicked_file($dropped_file);			

			}
			else {
				log::write('Dropped file : '.$dropped_file);
				$this->capture($dropped_file);
			}
		}

	}


	/**
	*	Populate the data fields
	*	
	*	@param	
	*	@return	
	*	
	*/
	function field_list_draw($field_id=FALSE) {

		$ts = new settings($this->config);
		$fields = $ts->getDataTypes();
		unset( $ts);


		
		$this->control['field_list']->clear();

		foreach ( $fields as $idx => $dat ) {
			$row_dat = array( $dat['id'], $dat['tag'], $dat['name'], $dat['unit'], $dat['description'] );
			$this->control['field_list']->append($row_dat);
		}

		if ( $field_id ) {
			// Make this customer selected as well
			$this->control['field_list']->set_selected('id',$field_id);
		}



	}


	function make_constants() {

		if ( ! is_dir(platform::app_data('Tazu-Timesheet-Manager') )) {
			mkdir(platform::app_data('Tazu-Timesheet-Manager'));
		}
		if(!defined('INIFILE')) 	define("INIFILE",		platform::app_data('Tazu-Timesheet-Manager')."config.ini");
		if(!defined('RECENTFILES')) define("RECENTFILES",	platform::app_data('Tazu-Timesheet-Manager')."recent.ini");
		if(!defined('SYSTEMSTATE')) define("SYSTEMSTATE",	platform::app_data('Tazu-Timesheet-Manager')."system.ini");

	}

	function open() {
		global $gl, $argv;

		$cnf = new conf_timesheet(INIFILE);

		$this->timesheets = platform::app_data('Tazu-Timesheet-Manager').'timesheets.db';
		$this->customers = platform::app_data('Tazu-Timesheet-Manager').'customers.db';
		$this->workers = platform::app_data('Tazu-Timesheet-Manager').'workers.db';
		$this->config = platform::app_data('Tazu-Timesheet-Manager').'config.db';
		
		// Load the text translations
		$c = new settings($this->config);
//		$this->data_types = $c->getDataTypes();
		unset($c);
		

		$this->init_window(APPSHORTNAME.' '.VER.'.'.BUILD);

		$font = $this->font;


		$this->add_control(new label($this->glade,'customers_tab'));
		$this->add_control(new label($this->glade,'workers_tab'));


		$this->add_control(new label_blue($this->glade,'select_customer_frame_label'));   //#0058EE
		$this->add_control(new customer_grid($this->glade,'ts_customers',$this));
		$this->add_control(new pushbutton($this->glade,'ts_edit'));
		$this->add_control(new pushbutton($this->glade,'ts_new'));
		$this->add_control(new pushbutton($this->glade,'ts_delete'));
		$this->add_control(new label($this->glade,'ts_new_lbl'));
		$this->add_control(new label($this->glade,'ts_edit_lbl'));
		$this->add_control(new label($this->glade,'ts_delete_lbl'));


		$this->add_control(new label_blue($this->glade,'select_project_frame_label'));   //#0058EE
		$this->add_control(new project_grid($this->glade,'proj_list',$this));
		$this->add_control(new pushbutton($this->glade,'proj_new'));
		$this->add_control(new pushbutton($this->glade,'proj_edit'));
		$this->add_control(new pushbutton($this->glade,'proj_delete'));
		$this->add_control(new label($this->glade,'proj_new_lbl'));
		$this->add_control(new label($this->glade,'proj_edit_lbl'));
		$this->add_control(new label($this->glade,'proj_delete_lbl'));


		$this->add_control(new label_blue($this->glade,'select_worker_frame_label'));   //#0058EE
		$this->add_control(new worker_grid($this->glade,'worker_list',$this));
		$this->add_control(new pushbutton($this->glade,'worker_new'));
		$this->add_control(new pushbutton($this->glade,'worker_edit'));
		$this->add_control(new pushbutton($this->glade,'worker_delete'));
		$this->add_control(new label($this->glade,'worker_new_lbl'));
		$this->add_control(new label($this->glade,'worker_edit_lbl'));
		$this->add_control(new label($this->glade,'worker_delete_lbl'));


		$this->add_control(new label_blue($this->glade,'select_field_frame_label'));   //#0058EE
		$this->add_control(new field_grid($this->glade,'field_list',$this));
		$this->add_control(new pushbutton($this->glade,'field_new'));
		$this->add_control(new pushbutton($this->glade,'field_edit'));
		$this->add_control(new pushbutton($this->glade,'field_delete'));
		$this->add_control(new label($this->glade,'field_new_lbl'));
		$this->add_control(new label($this->glade,'field_edit_lbl'));
		$this->add_control(new label($this->glade,'field_delete_lbl'));

		$this->add_control(new label_blue($this->glade,'select_xlate_frame_label'));   //#0058EE
		$this->add_control(new xlate_grid($this->glade,'xlate_list',$this));
		$this->add_control(new pushbutton($this->glade,'xlate_new'));
		$this->add_control(new pushbutton($this->glade,'xlate_edit'));
		$this->add_control(new pushbutton($this->glade,'xlate_delete'));
		$this->add_control(new label($this->glade,'xlate_new_lbl'));
		$this->add_control(new label($this->glade,'xlate_edit_lbl'));
		$this->add_control(new label($this->glade,'xlate_delete_lbl'));






		$this->customer_list_draw();		// Fill customer list
		$this->worker_list_draw();			// Fill worker list
		$this->field_list_draw();			// Fill worker list
		$this->xlate_list_draw();			// Fill worker list


		// Initial state of controls
		$this->task_name='';
		$this->cust_name='';
						
		$this->resetControls();
//		$this->enable('TS_NEW');
		
		// restore previous selections
		$customer_id = $cnf->ini['TIMESHEET']['customer_id'];
		$task_id = $cnf->ini['TIMESHEET']['task_id'];

		if ( $customer_id <> '' ) {

			$this->enable('TS_CUSTOMERS');
			$this->customer_list_draw($customer_id);				// Fill customer list
			$this->onCustomerClicked();							// Action as if clicked

		}				

		if ( $task_id <> '' ) {

			$this->enable(PROJ_LIST);
			$this->project_list_draw($customer_id,$task_id);		// Fill project list
			$this->onProjectClicked();							// Action as if clicked

		
		}				




	}


	/**
	*	onAddCustomer
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onAddCustomer() {

		$cust = new edit_customer('Edit Customer Details','new_container');
		$customer_id = $cust->edit(FALSE,$this->customers);
			
		// redraw the customer list
		if ( $customer_id ) $this->customer_list_draw($customer_id);
	
	}


	/**
	*	onAddProject
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onAddProject() {

		$customer_id = $this->selected_customer();

		$proj = new edit_project('Edit Project Details','new_container');
		$task_id = $proj->edit($customer_id,FALSE,$this->customers);
			
		// redraw the customer list
		if ( $task_id ) $this->project_list_draw($customer_id,$task_id);
	
	}

	/**
	*	onAddWorker
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onAddWorker() {


		$wrk = new edit_worker(settings::xlate('Edit [worker] Details'),'new_container');
		$id = $wrk->edit(FALSE,$this->workers);
			
		// redraw the customer list
		if ( $id ) $this->worker_list_draw($id);
	
	}


	/**
	*	onAddWorker
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onAddField() {

		$wrk = new edit_field(settings::xlate('Edit Data Field'),'data_field');
		$id = $wrk->edit(FALSE);
			
		// redraw the customer list
		if ( $id ) $this->field_list_draw($id);
	
	}



	/**
	*	onDeleteCustomer
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onDeleteCustomer() {


		// Selected customer
		$customer_id = $this->selected_customer();
		if ( ! $customer_id ) return;

						
        if(platform::yes_no(
        					settings::xlate('Are you sure that you want to delete this [[customer]] ?'), 
        					settings::xlate('Delete [customer]')
        					)) { 

			$ts = new Customers($this->customers);
			$ts->delCustomer($customer_id);
			$ts->close();
			unset( $ts );
							
			$this->customer_list_draw();
			$this->resetControls();
		}

	}

	/**
	*	onDeleteProject
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onDeleteProject() {


		// Selected customer
		$customer_id = $this->selected_customer();
		$task_id = $this->selected_task();
		if ( ! $task_id ) return;

						
        if(platform::yes_no("Are you sure that you want to delete this project ?", 'Delete Project')) { 

			$ts = new Customers($this->customers);
			$ts->delProject($task_id);
			$ts->close();
			unset( $ts );
							
			$this->project_list_draw($customer_id);
		}

	}


	/**
	*	onDeleteWorker
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onDeleteWorker() {

		$id = $this->selected_worker();
		if ( ! $id ) return;

						
        if(platform::yes_no("Are you sure that you want to delete this worker ?", 'Delete Worker')) { 

			$wrk = new Workers($this->workers);
			$wrk->delWorker($id);
			$wrk->close();
			unset( $wrk );
							
			$this->worker_list_draw(FALSE);
		}

	}


	/**
	*	onDeleteField
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onDeleteField() {

		$id = $this->selected_field();
		if ( ! $id ) return;

						
        if(platform::yes_no("Are you sure that you want to delete this data field ?", 'Delete Data Field')) { 

			$wrk = new settings($this->config);
			$wrk->delDat($id);
			unset( $wrk );
			$this->field_list_draw(FALSE);
		}

	}




	/**
	*	onEditCustomer
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onEditCustomer() {


		// Selected customer
		$customer_id = $this->selected_customer();


		if ( $customer_id ) {
			$cust = new edit_customer('Edit Customer Details','new_container');
			$ok = $cust->edit($customer_id,$this->customers);
			
			// redraw the customer list
			if ( $ok ) $this->customer_list_draw();
		}
					
	
	}

	/**
	*	onEditProject
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onEditProject() {


		// Selected customer
		$customer_id = $this->selected_customer();
		$task_id = $this->selected_task();


		if ( $task_id ) {
			$proj = new edit_project('Edit Project Details','new_container');
			$ok = $proj->edit($customer_id,$task_id,$this->customers);
			
			// redraw the customer list
			if ( $ok ) $this->project_list_draw($customer_id,$task_id);
		}
					
	
	}


	/**
	*	onEditWorker
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onEditWorker() {

		$id = $this->selected_worker();


		if ( $id ) {
			$wrk = new edit_worker(settings::xlate('Edit [worker] Details'),'new_container');
			$ok = $wrk->edit($id,$this->workers);
			
			// redraw the customer list
			if ( $ok ) $this->worker_list_draw($id);
		}
					
	
	}


	/**
	*	onEditField
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onEditField() {

		$id = $this->selected_field();


		if ( $id ) {
			$wrk = new edit_field(settings::xlate('Edit Data Field'),'data_field');
			$ok = $wrk->edit($id);
			
			// redraw the customer list
			if ( $ok ) $this->field_list_draw($id);
		}
						
	}

	/**
	*	onEditField
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onEditXlate() {

		$id = $this->selected_xlate();


		if ( $id ) {
			$wrk = new edit_xlate(settings::xlate('Edit Terminology'),'xlate');
			$ok = $wrk->edit($id);
			
			// redraw the customer list
			if ( $ok ) $this->xlate_list_draw($id);
		}
						
	}



	/**
	*	Customer in customer list selected/clicked
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onCustomerClicked() {

		$cnf = new conf_timesheet(INIFILE);
		$customer_id = $this->control['ts_customers']->get_selected('id');
		
		if ( $customer_id ) {

			// Customer selected
					
			// Record last selected customer/project/entry
			$cnf->set_value('TIMESHEET','customer_id',$customer_id);		
			$cnf->set_value('TIMESHEET','task_id',FALSE);		
			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
			$cnf->set_value('TIMESHEET','data_id',FALSE);		

		}
		else {
			// No selection
			// Record last selected customer/project/entry
			$cnf->set_value('TIMESHEET','customer_id',FALSE);		
			$cnf->set_value('TIMESHEET','task_id',FALSE);		
			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
			$cnf->set_value('TIMESHEET','data_id',FALSE);		
		}

		$cnf->save_ini();

		$this->customers();
		$this->projects();
	}
		

	function onProjectClicked() {

		$cnf = new conf_timesheet(INIFILE);

		$task_id = $this->control['proj_list']->get_selected('id');
				
		if ( $task_id ) {

			// Record last selected customer/project/entry
			$cnf->set_value('TIMESHEET','task_id',$task_id);		
			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
			$cnf->set_value('TIMESHEET','data_id',FALSE);		
					
		}
		else {
			// No selection
			// Record last selected customer/project/entry
			$cnf->set_value('TIMESHEET','task_id',FALSE);		
			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
			$cnf->set_value('TIMESHEET','data_id',FALSE);		
		}
		$cnf->save_ini();

		$this->projects();
	}
	

	function onWorkerClicked() {

		$cnf = new conf_timesheet(INIFILE);

		$id = $this->control['worker_list']->get_selected('id');
				
		if ( $id ) {

			// Record last selected customer/project/entry
//			$cnf->set_value('TIMESHEET','task_id',$task_id);		
//			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
//			$cnf->set_value('TIMESHEET','data_id',FALSE);		
					
		}
		else {
			// No selection
			// Record last selected customer/project/entry
//			$cnf->set_value('TIMESHEET','task_id',FALSE);		
//			$cnf->set_value('TIMESHEET','entry_id',FALSE);		
//			$cnf->set_value('TIMESHEET','data_id',FALSE);		
		}
//		$cnf->save_ini();

		$this->workers();
	}
	



	function projects() {


		$task_id = $this->selected_task();
		if ( $task_id ) {
			// Task selected
			$task_code = $this->control['proj_list']->get_selected('task_code');
			$task_name = $this->control['proj_list']->get_selected('task');
			$this->task_name = $this->control['proj_list']->get_selected('task');

			$this->enable('PROJ_NEW');
			$this->enable('PROJ_EDIT');
			$this->enable('PROJ_DELETE');
			
		}
		else {
			// no tasks selected
			$this->task_name = '';

			$this->enable('PROJ_NEW');
			$this->disable('PROJ_EDIT');
			$this->disable('PROJ_DELETE');
		}

		$customer_id = $this->selected_customer();
		if ( $customer_id ) {
			// Customer Selected
			$customer_name = $this->control['ts_customers']->get_selected('name');
				
			// Load the Project/Task frame
			$this->enable('PROJ_LIST');
		}
		else {
			// No customer selected		
			$this->cust_name = '';
		}


	}


	/**
	*	Populate the project list
	*	
	*	@param	
	*	@return	
	*	
	*/
	function project_list_draw($customer_id,$task_id=FALSE) {

		$ts = new Customers($this->customers);
		$projects = $ts->getProjectList($customer_id);

		$this->control['proj_list']->clear();

		foreach ( $projects as $idx => $dat ) {
			$row_dat = array( $dat['id'], $dat['task_code'], $dat['task'], $ts->elapsed($dat['estimate_time']), '' );
			$this->control['proj_list']->append($row_dat);
		}

		$ts->close();
		unset( $ts);


		// Make this task/project selected as well
		$this->project_list_selected($customer_id ,$task_id );

	}

	function project_list_selected($customer_id,$task_id=FALSE) {

		if ( $task_id ) {
			$this->control['proj_list']->set_selected('id',$task_id);
		}

//		$this->listview_drawn['projects'] = TRUE;

	}


	function resetControls() {
		// Initial state of controls

		// Customers
		$this->enable('TS_NEW');					// Always enabled
		$this->disable('TS_EDIT');
		$this->disable('TS_DELETE');


		$this->task_name='';
		$this->cust_name='';
								
		// Project/Tasks List
		$this->control['proj_list']->clear();
		$this->disable('proj_list');
		$this->disable('PROJ_EDIT');
		$this->disable('PROJ_DELETE');
		$this->disable('PROJ_NEW');

		// Workers
		$this->enable('WORKER_NEW');					// Always enabled
		$this->disable('WORKER_EDIT');
		$this->disable('WORKER_DELETE');
		
	}

	function selected_customer() {
		$customer_id = $this->control['ts_customers']->get_selected('id');
		return( $customer_id );
	}
	
	function selected_task() {
		$task_id = $this->control['proj_list']->get_selected('id');
		return( $task_id );
	}

	function selected_worker() {
		$id = $this->control['worker_list']->get_selected('id');
		return( $id );
	}

	function selected_field() {
		$id = $this->control['field_list']->get_selected('id');
		return( $id );
	}

	function selected_xlate() {
		$id = $this->control['xlate_list']->get_selected('id');
		return( $id );
	}


	/**
	*	Populate the worker list
	*	
	*	@param	
	*	@return	
	*	
	*/
	function worker_list_draw($worker_id=FALSE) {

		$ts = new Workers($this->workers);
		$workers = $ts->getWorkers();
		$ts->close();
		unset( $ts);


		
		$this->control['worker_list']->clear();

		if ( is_array($workers) ) {
			foreach ( $workers as $idx => $dat ) {
				$row_dat = array( $dat['id'], $dat['username'], $dat['first_name'], $dat['last_name'], $dat['email'], $dat['timesheet_file'] );
				$this->control['worker_list']->append($row_dat);
			}
		}
		
//$this->control['ts_customers']->widget->set_grid_lines(Gtk::TREE_VIEW_GRID_LINES_VERTICAL); 

		if ( $worker_id ) {
			// Make this customer selected as well
			$this->control['worker_list']->set_selected('id',$worker_id);
		}



	}



	function workers() {


		$id = $this->selected_worker();

		if ( $id ) {
			// Task selected
//			$task_code = $this->control['proj_list']->get_selected('task_code');
//			$task_name = $this->control['proj_list']->get_selected('task');
//			$this->task_name = $this->control['proj_list']->get_selected('task');

			$this->enable('worker_NEW');
			$this->enable('worker_EDIT');
			$this->enable('worker_DELETE');
			
		}
		else {
			// no tasks selected
			$this->task_name = '';

			$this->enable('worker_NEW');
			$this->disable('worker_EDIT');
			$this->disable('worker_DELETE');
		}


	}

	/**
	*	Populate the text translations
	*	
	*	@param	
	*	@return	
	*	
	*/
	function xlate_list_draw($field_id=FALSE) {

		$ts = new settings($this->config);
		$fields = $ts->getXlate();
		unset( $ts);

		$this->control['xlate_list']->clear();

		foreach ( $fields as $idx => $dat ) {
			$row_dat = array( $dat['id'], $dat['default_txt'], $dat['replace'] );
			$this->control['xlate_list']->append($row_dat);
		}

		if ( $field_id ) {
			// Make this customer selected as well
			$this->control['xlate_list']->set_selected('id',$field_id);
		}

	}




}



?>